package com.cyz.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyz.dao.ProductDao;
import com.cyz.dao.SupplierDao;
import com.cyz.dao.TypeDao;
import com.cyz.entity.ProductEntity;
import com.cyz.entity.SupplierEntity;
import com.cyz.entity.TypeEntity;
import com.cyz.vo.ProductVo;
import org.apache.ibatis.ognl.ASTConst;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 *
 *
 * @author chenyizhen
 * @email sunlightcs@gmail.com
 * @date 2023-12-18 15:02:10
 */
@RestController
@RequestMapping("user/product")
public class ProductController {

    @Autowired
    ProductDao productDao;

    @Autowired
    TypeDao typeDao;

    @Autowired
    SupplierDao supplierDao;

    @Autowired
    TypeController typeController;

    @Autowired
    SupplierController supplierController;

    @PostMapping("/select")
    public List<ProductVo> list(){
        List<ProductVo> collect = productDao.selectList(new QueryWrapper<>()).stream().map(item -> {
            ProductVo productVo = new ProductVo();
            BeanUtils.copyProperties(item, productVo);
            TypeEntity typeEntity = typeDao.selectById(item.getTypeId());
            if (typeEntity != null) {
                productVo.setTypeName(typeEntity.getName());
            }

            SupplierEntity supplierEntity = supplierDao.selectById(item.getSupplierId());
            if (supplierEntity != null) {
                productVo.setSupperName(supplierEntity.getName());
            }

            return productVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @PostMapping("/update")
    public String update(int id, String name, String typeName, String supperName){
        ProductEntity productEntity = productDao.selectById(id);
        if (productEntity == null){
            return "该商品不存在";
        }
        productEntity.setName(name);
        TypeEntity typeEntity = typeController.selectByName(typeName);
        productEntity.setTypeId(typeEntity.getId());

        SupplierEntity supplierEntity = supplierController.selectByName(supperName);
        productEntity.setTypeId(supplierEntity.getId());

        int update = productDao.updateById(productEntity);

        return update == 0 ? "更新失败" : "更新成功";
    }

    @PostMapping("/selectById/{id}")
    public String selectById(@PathVariable int id){
        return productDao.selectById(id).getName();
    }

    @PostMapping("/deleteById/{id}")
    public String deleteById(@PathVariable int id){
        int i = productDao.deleteById(id);
        return i == 0 ? "删除失败" : "删除成功";
    }

    @PostMapping("/insert")
    public String insertById(String name, String typeName, String supperName){
        ProductEntity productEntity = new ProductEntity();
        productEntity.setName(name);
        productEntity.setTypeId(typeController.selectByName(typeName).getId());
        productEntity.setSupplierId(supplierController.selectByName(supperName).getId());
        int insert = productDao.insert(productEntity);

        return insert == 0 ? "新增失败" : "新增成功";
    }
}
